<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$basic=$this->plugin_setting('basic');
	$return['list']=array();
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid']));
	set_html('title',$basic['title']?$basic['title']:'球场预定');
	$return['member']=cfc('core')->get_member($_SESSION['uid']);
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['pcd']=$this->session('pcd')?:array(
		'province' =>'',
		'city' =>'',
		'district' =>'',
	);
	$return['params']=array('pcd'=>$return['pcd'],'type'=>1,'storetype'=>0);
	$return['sharedata']=array(
			'title'=>$basic['share_title']?:'球场预定',
			'desc'=>$basic['share_desc']?:'球场预定，在线预定足球场，羽毛球场！',
			'link'=>custom_url('jy_qiuyuyue/index//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	if(intval($basic['index_tpl'])){
		$return['htmls']=$this->template_url('index_a'.$basic['index_tpl']);
	}
	$return['groups']=pdo_getall('jy_qiuyuyue_serve_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'','paixu desc,id desc');
	$return['pluginset']['searchkeyword']=str_array($return['pluginset']['searchkeyword'],'|');
	if(!$_SESSION['istanxieyi']){
		$tanxieyi=$this->plugin_setting('tanxieyi');
		if($tanxieyi['isopen']){
			$return['tanxieyi']=$tanxieyi;
			$return['show_tanxieyi']=true;
			$_SESSION['istanxieyi']=1;
		}
	}
	exi($return);
}
if($_RQ['op']=='list'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1');
	if(trim($_RQ['keyword'])){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if(intval($_RQ['type'])==1){
		$storeids = pdo_getall('jy_qiuyuyue_server_order',array('mid'=>$_SESSION['uid']),array('store_id'),'store_id');
		if($storeids){
			$storeids=array_keys($storeids);
			$params['id']=$storeids;
		}else{
			$return['list']=array();
			exi($return);
		}	
	}
	if($_RQ['province'] && $_RQ['city']){
			$pcd=array('province'=>$_RQ['province'],'city'=>$_RQ['city']);
			$this->session('pcd',$pcd);
			$code=$this->model('model')->get_area_code($pcd);
			if($code['area_code']){
				$params['code']=trim($code['area_code']);
			}
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 500;
	$total='';
	$list=pdo_getslice('jy_qiuyuyue_store',$params,array($pindex,$psize),$total,$fields,'','display desc, id desc');	
	foreach($list as &$v){
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['time']=json_decode($v['time'],true);
			if($v['lat_lng']){				
				$v['distance']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
				$v['distance']=intval($v['distance']/1000);
			}
			$v['ordernum']=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$v['id']));
	}
	paixu($list,'distance');
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	//设置场馆
	if(intval($_RQ['store_id'])){
		$store_id = intval($_RQ['store_id']);
		$this->session('store_id',$store_id);
	}elseif($list[0]['id']){
		$this->session('store_id',$list[0]['id']);
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getlist'){
	$post=get_postdata();
	$post['page']=$post['page']?:$_RQ['page'];
	$pindex = max(1, intval($post['page']));
	$psize = 16;
	$total='';
	$params1=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1);
	$params2=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1,'islock'=>0);
	if(trim($_RQ['keyword'])){
		$params1['title like']='%'.trim($_RQ['keyword']).'%';
		$params2['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	$post['province']=$post['province']?:trim($_RQ['province']);
	$post['city']=$post['city']?:trim($_RQ['city']);
	if($post['province']){
		if($post['city']){
			//查询商户id
			$merch_id=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'city'=>$post['city']),'id');
			if($merch_id>0){
				$params1['merchid']=$merch_id;
				$params2['merchid']=$merch_id;
			}
		}else{
			$merch_id=pdo_getcolumn('jy_qiuyuyue_store',array('uniacid'=>$_SESSION['uniacid'],'province'=>$post['province']),'id');
			if($merch_id>0){
				$params1['merchid']=$merch_id;
				$params2['merchid']=$merch_id;
			}

		}
	}
	$worker_show=pdo_getslice('jy_qiuyuyue_store_worker',$params2,array($pindex,$psize),$total,$fields,'','id desc');
	$serve_show=pdo_getslice('jy_qiuyuyue_serve',$params1,array($pindex,$psize),$total,$fields,'','paixu desc, id desc');
	$return['serve_show']=$serve_show;
	$return['list']=$return['worker_show']=$worker_show;
	exi($return);
}
if($_RQ['op']=='getworkerlist'){
	$setting=$this->plugin_setting('basic');
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$pindex = max(1, intval($post['page']));
	$psize = 1000;
	$total='';
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','isshow'=>1,'islock'=>0);
	if(trim($_RQ['keyword'])){
		$params['name like']='%'.trim($_RQ['keyword']).'%';
		$return['params']['keyword']=trim($_RQ['keyword']);
	}
	if(isset($_RQ['status'])){
		if(intval($_RQ['status'])==2){
			$params['islock']=1;
		}elseif(intval($_RQ['status'])==3){
			$params['status']=0;
		}
		$return['params']['status']=trim($_RQ['status']);
	}
	$list=pdo_getslice('jy_qiuyuyue_store_worker',$params,array($pindex,$psize),$total,$fields,'','looks desc');
	foreach($list as $k=>&$v){
		$v['headimg']=tomedia($v['headimg']);
		$v['lat_lng']=$v['lat_lng']?:'{}';
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['tags']=$v['tags']?:$setting['tags'];
		$v['tags']=explode('|',$v['tags']);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		$v['sales']=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'status >'=>0,'worker_id'=>$v['id']),'id');
		$v['starall']=pdo_getsum('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'worker_id'=>$v['id']),'star');
		$v['starcount']=pdo_count('jy_qiuyuyue_server_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>4,'worker_id'=>$v['id'],'star >'=>0),'id');
		$v['stars']=$v['starall']/$v['starcount'];
		$v['stars']=$v['stars']?:5;
		if($v['islock']==0 && $v['status']==1){
			$v['statustitle']='可接单';
		}elseif($v['islock']==1 && $v['status']==1){
			$v['statustitle']='服务中';
		}else{
			$v['statustitle']='休息中';
		}
		
	}
	if(trim($_RQ['order'])){
		$return['params']['order']=trim($_RQ['order']);
		if(trim($_RQ['order'])=='default'){
			paixu($list,'juli');
		}elseif(trim($_RQ['order'])=='star'){
			paixu($list,'stars');
		}elseif(trim($_RQ['order'])=='sales'){
			paixu($list,'sales');
		}elseif(trim($_RQ['order'])=='looks'){
			paixu($list,'looks');
		}
	}else{
		paixu($list,'juli');
	}
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	exi($return);
}	
?>